package com.neusoft.yuejian12.yuejian_12.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.javabean.EnterTrainBean;
import com.neusoft.yuejian12.yuejian_12.javabean.TrainBean;
import com.neusoft.yuejian12.yuejian_12.javabean.UserBean;
import com.neusoft.yuejian12.yuejian_12.model.TrainModel;
import com.neusoft.yuejian12.yuejian_12.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterTrainActivity extends AppCompatActivity {
    //声明
    private ImageView left;
    private Button entertrain_button;
    private int u_id;
    private int t_id;
    private TextView t_name,t_price,t_type,u_name,u_phone,buy_num,total_price;
    private ImageView  addbutton,reducebutton;
    private int num;
    private Double sum;
    private Double tprice;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_train);
        t_id=getIntent().getIntExtra("t_id",-1);
        initViews();//初始化
        initEvents();//点击事件处理
        //getdata();
    }

    private void initViews() {
        t_name = (TextView) findViewById(R.id.name);

        u_name = (TextView) findViewById(R.id.u_name);
        u_phone = (TextView) findViewById(R.id.u_phone);
        t_price = (TextView) findViewById(R.id.price);

        t_type = (TextView) findViewById(R.id.a_address);
        entertrain_button = (Button) findViewById(R.id.enterAct_button);

        left= (ImageView) findViewById(R.id.left);
    }

    private void initEvents() {
        //返回上一页
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id", 0);

        UserModel userModel = new UserModel();
        Call callu = userModel.getUserInfo(u_id);
        callu.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean user = response.body();
                u_name.setText(user.getU_name());
                u_phone.setText(user.getU_phone());
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
            }
        });


        TrainModel trainModel = new TrainModel();
        Call<TrainBean> call = trainModel.getDetail(t_id);
        call.enqueue(new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                TrainBean data = (TrainBean) response.body();  //获取数据
                String tname = data.getT_name();
                int tprice = data.getT_price();
                int ttype=data.getT_type();
                t_name.setText(tname);
                t_price.setText("￥"+ tprice+"");
                if(ttype==0)
                {
                    t_type.setText("入门");
                }
                else if(ttype==1){
                    t_type.setText("菜鸟");
                }
                else if(ttype==2){
                    t_type.setText("进阶");
                }
                else {
                    t_type.setText("高级");
                }
              //  t_type.setText(ttype+"");

            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {

            }
        });


        entertrain_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel = new TrainModel();
                Call<EnterTrainBean> call = trainModel.addtrainn(u_id,t_id);
                call.enqueue(new Callback<EnterTrainBean>() {
                    @Override
                    public void onResponse(Call<EnterTrainBean> call, Response<EnterTrainBean> response) {
                        EnterTrainBean enterTrainBean=response.body();
                        if ("1".equals(enterTrainBean.getSuccess())) {
                            Toast.makeText(EnterTrainActivity.this, "报名成功", Toast.LENGTH_LONG).show();

                        }else if("2".equals(enterTrainBean.getSuccess())){
                            Toast.makeText(EnterTrainActivity.this, "请不要重复报名", Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(EnterTrainActivity.this, "失败", Toast.LENGTH_LONG).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<EnterTrainBean> call, Throwable t) {

                    }
                });
            }
        });


    }
}

